####################################################################
## author:    Robert A. Huber, Lukas P. Fesenfeld & Thomas Bernauer
## contact:   robert.huber@ir.gess.ethz.ch
## file name: erc_popcli_analyses_new.R
## Context:   ERC Populism Project on Public Support for Climate Politics
## started:   2019-07-30
## Summary:   runs R-Scripts
######################################################################

df$cliEco_rec <- scale(df$cliEco)

# Interaction w/ Party ID and populism ####

controls_INT <- " + age + gender + educGrp + inc + urbanRural + ccKnow + region + employGrp +  partyID + pop"

#Models
m_Con_ID <- lm_robust(formula(paste0("climConcern ~ trt*partyID*pop" , controls_INT)), df)
summary(m_Con_ID)

m_cliEco_ID <- lm_robust(formula(paste0("cliEco_rec ~ trt*partyID*pop" , controls_INT)), df)
summary(m_cliEco_ID)

m_wtp_ID <- lm_robust(formula(paste0("wtp ~ trt*partyID*pop" , controls_INT)), df)
summary(m_wtp_ID)

# Populism plot ####
#Extract Margins
df_con <- data.frame(summary(margins(m_Con_ID, variables = "pop")))

df_con <- cbind(df_con[1], partyID = "P", df_con[2:ncol(df_con)])

df_con_ID <- data.frame(summary(margins(m_Con_ID, at = list(partyID = c("R", "D", "I")), variables = "pop")))

df_con_pop <- rbind(df_con, df_con_ID)

#Extract Margins
df_cliEco <- data.frame(summary(margins(m_cliEco_ID, variables = "pop")))

df_cliEco <- cbind(df_cliEco[1], partyID = "P", df_cliEco[2:ncol(df_cliEco)])

df_cliEco_ID <- data.frame(summary(margins(m_cliEco_ID, at = list(partyID = c("R", "D", "I")), variables = "pop")))

df_cliEco_pop <- rbind(df_cliEco, df_cliEco_ID)

#Extract Margins
df_wtp <- data.frame(summary(margins(m_wtp_ID, variables = "pop")))

df_wtp <- cbind(df_wtp[1], partyID = "P", df_wtp[2:ncol(df_wtp)])

df_wtp_ID <- data.frame(summary(margins(m_wtp_ID, at = list(partyID = c("R", "D", "I")), variables = "pop")))

df_wtp_pop <- rbind(df_wtp, df_wtp_ID)

#df_wtp_ID <- subset(data.frame(summary(df_wtp_ID)), factor %in% c("trtElite Cares Climate", "trtElite Cares Neutral", "trtElite Self-Interest Climate", "trtElite Self-Interest Neutral"))

df_pop <- rbind(df_con_pop, df_cliEco_pop, df_wtp_pop)
df_pop$partyID <- factor(df_pop$partyID, levels = c("P", "D","I", "R"),
                         labels = c("Pooled", "Dem", "Ind", "Rep"))
df_pop$dv <- factor(rep(c("Concern", "Climate policy\nvs\neconomic growth", "Willingness to pay"), each = 4),
                    levels = c("Concern", "Climate policy\nvs\neconomic growth", "Willingness to pay"))

ggplot(df_pop, aes(x=partyID, y=AME, colour = partyID, shape = partyID)) +
  geom_pointrange(aes(ymin = lower, ymax = upper), position = position_dodge(width = .5)) +
  geom_hline(yintercept = 0, lty = "dashed") + 
  facet_grid(~dv) +
  theme_tufte() + 
  #theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + 
  labs(x="\nPartisanship", y= "Average Marginal Effect\n") +
  theme(text = element_text(size=20), axis.text.x = element_text(size=16)) +
  scale_color_manual(values = c("black","blue", "orange", "red")) +
  scale_shape_manual(values = c(15:18)) +
  theme(legend.position = "none") +
  NULL

ggsave(filename = "./figures/models/PCPol_Figure1.pdf", 
       width = 23,
       height = 15,
       units = "cm",
       device = "pdf")

ggsave(filename = "./figures/models/PCPol_Figure1.png", 
       width = 23,
       height = 15,
       units = "cm",
       device = "png")


# Trt plot w/o interaction ####
#Extract Margins
df_con <- data.frame(summary(margins(m_Con_ID, variables = "trt")))

#Extract Margins
df_cliEco <- data.frame(summary(margins(m_cliEco_ID, variables = "trt")))

#Extract Margins
df_wtp <- data.frame(summary(margins(m_wtp_ID, variables = "trt")))

#df_wtp_ID <- subset(data.frame(summary(df_wtp_ID)), factor %in% c("trtElite Cares Climate", "trtElite Cares Neutral", "trtElite Self-Interest Climate", "trtElite Self-Interest Neutral"))

df_trt <- rbind(df_con, df_cliEco, df_wtp)

df_trt$dv <- factor(rep(c("Concern", "Climate policy\nvs\neconomic growth", "Willingness to pay"), each = 4),
                    levels = c("Concern", "Climate policy\nvs\neconomic growth", "Willingness to pay"))

df_trt$iv <- factor(rep(c("Cares Climate", "Cares Neutral", "Self-Int. Climate", "Self-Int. Neutral"), 3),
                    levels = c("Cares Climate", "Cares Neutral", "Self-Int. Climate", "Self-Int. Neutral"))

ggplot(df_trt, aes(x=dv, y=AME, shape = iv)) +
  geom_pointrange(aes(ymin = lower, ymax = upper), position = position_dodge(width = .5)) +
  geom_hline(yintercept = 0, lty = "dashed") + 
  guides(shape = guide_legend(nrow = 2)) +
  theme_tufte() + 
  labs(x="\nOutcome", y="Average Marginal Effect\n") +
  theme(text = element_text(size=20), axis.text.x = element_text(size = 12)) +
  theme(legend.position = "bottom") +
  scale_shape_discrete("Treatment") +
  NULL

ggsave(filename = "./figures/models/PCPol_Figure2.pdf", 
       width = 23,
       height = 15,
       units = "cm",
       device = "pdf")

ggsave(filename = "./figures/models/PCPol_Figure2.png", 
       width = 23,
       height = 15,
       units = "cm",
       device = "png")
